package com.example.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.bean.Admin;
import com.example.server.bean.Menu;
import com.example.server.dao.MenuDao;
import com.example.server.service.IMenuService;
import com.example.server.util.CurrentAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;

import static com.example.server.util.RedisConstant.MENU_ID_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 别怿言
 * @since 2023-02-03
 */
@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements IMenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public List<Menu> getMenusByAdminId() {
        Integer adminid = CurrentAdmin.getCurrentAdmin().getId();
        String menukey=MENU_ID_KEY+adminid;
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        List<Menu> menus = (List<Menu>) operations.get(menukey);
        if(CollectionUtils.isEmpty(menus)){
            menus = menuDao.getMenusByAdminId(adminid);
            operations.set(menukey,menus);
        }
        return menus;
    }

    @Override
    public List<Menu> getMenusWithRole() {
        return menuDao.getMenusWithRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuDao.getAllMenus();
    }
}
